package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.ResourceDto;
import com.zzyl.entity.Resource;
import com.zzyl.mapper.ResourceMapper;
import com.zzyl.service.ResourceService;
import com.zzyl.utils.EmptyUtil;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeItemVo;
import com.zzyl.vo.TreeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    //资源列表查询
    @Override
    public List<ResourceVo> resourceList(ResourceDto resourceDto) {
        List<Resource> resourceLists = resourceMapper.selectList(resourceDto);
        List<ResourceVo> resourceVos = BeanUtil.copyToList(resourceLists, ResourceVo.class);
//        resourceVos.forEach(v -> v.setCreateDay(LocalDateTimeUtil.format(v.getCreateTime(), "yyyy-MM-dd")));
        return resourceVos;
    }
    //资源树形
    @Override
    public TreeVo resourceTreeVo(ResourceDto resourceDto) {

        ResourceDto dto = ResourceDto.builder().build();

        List<Resource> resourceList = resourceMapper.selectList(dto);
        log.info("资源数量：{}",resourceList.size());
        if (resourceList == null||resourceList.size() == 0){
            throw new RuntimeException("资源信息未定义！！");
        }
        Resource root = new Resource();
        root.setResourceNo(SuperConstant.ROOT_PARENT_ID);
        root.setResourceName("智慧养老");
//        TODO 按照root根节点信息，将上面的resourceList 转成items
//         resourceList只是一个列表，数据和数据之间关系层级不明显
//         转成items后，出现俄罗斯套娃，嵌套关系明显，就能实现树级结构
        List<TreeItemVo> itemsvo = new ArrayList<>();

        recursionTreeItem(itemsvo,root,resourceList);

        TreeVo treeVo = new TreeVo(itemsvo);

        return treeVo;
    }

    private void recursionTreeItem(List<TreeItemVo> itemsvo, Resource root, List<Resource> resourceList) {

        TreeItemVo rootVo = TreeItemVo.builder()
                .id(root.getResourceNo())
                .label(root.getResourceName())
                .build();
        // 将所有资源遍历，一一的问，你的爸爸是我吗？
        List<Resource> rootChildVo = resourceList.stream().filter(item -> item.getParentResourceNo().equals(root.getResourceNo()))
                .collect(Collectors.toList());
        //判断儿子s是否是空的
        if (!EmptyUtil.isNullOrEmpty(rootChildVo)){
            List<TreeItemVo> childList =new ArrayList<>();
            //问儿子，你有儿子吗？
            rootChildVo.forEach(item->{
                //一个一个的问
                recursionTreeItem(childList,item,resourceList);
            });
            rootVo.setChildren(childList);
        }
        itemsvo.add(rootVo);
    }

    //资源添加
    @Override
    public Object createResource(ResourceDto resourceDto) {

        return null;
    }
}
